<?php

namespace App\HttpController\XAppSystem\Pwd\Admin;


use App\Common\OA\OA;
use App\HttpController\ControllerAdmin;
use App\Model\t_s_account;
use XWX\Common\Validate\Validate;

class PwdEdit extends ControllerAdmin
{
    public function index()
    {
        $old_pwd = $this->reqParam('old_pwd');
        $new_pwd = $this->reqParam('new_pwd');

//        var_dump($this->request()->getRequestParam());

        $v = new Validate();
        $v->addColumn('old_pwd')->required('请输入原密码');
        $v->addColumn('new_pwd')->required('请输入新密码');
        if (!$v->validate($this->request()->getRequestParam()))
        {
            return $this->funcWriteValidate($v->getErrors());
        }

        $app_user = $this->funcGetOAUser();

        $old_pwd_enc = OA::funcGetEncPwd($this->funcGetX(), $old_pwd);

        $entity_acc = $app_user->funcGet__acc();
        if ($entity_acc->acc_pwd != $old_pwd_enc)
        {
            return $this->funcWriteErr(-100, '原密码不正确');
        }

        $new_pwd_enc = OA::funcGetEncPwd($this->funcGetX(), $new_pwd);
        $this->db()
            ->where('acc_id', $entity_acc->acc_id)
            ->update(t_s_account::table(), [
                'acc_pwd' => $new_pwd_enc,
            ]);


        return $this->funcWriteCommonR();
    }
}